from PyQt6.QtWidgets import QGroupBox, QLabel,QDialog,QVBoxLayout,QPushButton,QMessageBox
from PyQt6.QtWidgets import QLineEdit,QHBoxLayout,QComboBox
from PyQt6.QtCore import QSize,Qt
from PyQt6.QtGui import QIntValidator
from db.DbmsDao import DbmsDao

class Ui_Dialog(QDialog):

    def __init__(self,parent,table_name,fileds,callback=None):
        super(Ui_Dialog,self).__init__(parent)
        self.callback_update_ui=callback
        self.fileds=fileds
        self.table_name=table_name

        self.setupUi()
        

    def setupUi(self):
        self.setWindowTitle(f"添加'{self.table_name}'表数据")
        self.resize(500, 400)

        layout=QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)

        groupbox= QGroupBox()
        groupbox.setMaximumWidth(480)
        groupbox.setTitle('输入表数据')

        layout_table = QVBoxLayout()
        layout_table.setAlignment(Qt.AlignmentFlag.AlignTop)

        for filed in self.fileds:
          
            layout_column = QHBoxLayout()
            layout_column.setAlignment(Qt.AlignmentFlag.AlignLeft)
            label=QLabel(f'{filed[0]}')
            label.setFixedSize(QSize(60,30))
            layout_column.addWidget(label)
            edit_value=QLineEdit()
            edit_value.setPlaceholderText(f'{filed[1]}|{filed[8]}')
            if filed[6]=='auto_increment' or filed[5]=='CURRENT_TIMESTAMP':
                edit_value.setEnabled(False)
            edit_value.setMaxLength(32)
            layout_column.addWidget(edit_value)
            layout_table.addLayout(layout_column)

        groupbox.setLayout(layout_table)

        layout.addWidget(groupbox)

        #底部按钮
        layout_btn=QHBoxLayout()
        layout_btn.setAlignment(Qt.AlignmentFlag.AlignHCenter)

        btn_save=QPushButton('保存')
        btn_save.setFixedSize(QSize(150,30))
        layout_btn.addWidget(btn_save)
        btn_save.clicked.connect(self.on_clicked_btn_save)

        btn_cancel=QPushButton('取消')
        btn_cancel.setFixedSize(QSize(150,30))
        layout_btn.addWidget(btn_cancel)
        btn_cancel.clicked.connect(self.on_clicked_btn_cancel)
        
        layout.addLayout(layout_btn)

        self.setLayout(layout)


    def on_clicked_btn_save(self):
        name=self.edit_name.text()
        type=self.combobox_type.currentText()
        host=self.edit_host.text()
        port=self.edit_port.text()
        user_name=self.edit_username.text()
        password=self.edit_password.text()
        remark=self.edit_remark.text()
        
        if self.c_id:
            
            result= DbmsDao.update_db_info(self.c_id,name,type,host,port,user_name,password,remark)
            if result == False:
                QMessageBox.warning(self,'错误提示','数据更新失败')
        else :
            result= DbmsDao.insert_db_info(name,type,host,port,user_name,password,remark)
            if result == False:
                QMessageBox.warning(self,'错误提示','数据插入失败')

        if self.callback_update_ui:
            self.callback_update_ui()

        self.close()

    def on_clicked_btn_cancel(self):
        self.close()
